﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EntityModel
{
    public partial class BulkOrder
    {


        public ProductDimension Dimension { get; set; }

        public int IDSQuantityAvail
        {
            get
            {
                if (this.IsShownInLGDCatalog.HasValue && this.IsShownInLGDCatalog.Value)
                {
                    return GetQuantityAvailableCatalog(this.QuantityAvailable, this.PackQty);
                }
                else
                {
                    return this.QuantityAvailable;
                }
            }
        }

        public int QuantityAvailable { get; set; }

        public string QtyAvailDescription
        {
            get
            {
                string result = string.Empty;

                if (this.IsShownInLGDCatalog.HasValue && this.IsShownInLGDCatalog.Value)
                {
                    if (this.PackQty.HasValue && !string.IsNullOrEmpty(this.CatPackDesc))
                    {
                        result = GetBOPackType2Meaning(this.CatPackDesc);
                    }
                }
                else
                {
                    if (this.QuantityAvailable != int.MinValue)
                    {
                        result = GetBOPackTypeMeaning(this.CatPackDesc);
                    }
                }

                return result;
            }
        }

        public string TypeCode
        {
            get
            {
                switch (this.Type)
                {
                    case 1:
                        return "OD";  // Ordered
                    case 2:
                        return "SC"; // Stock Clear
                    case 3:
                        return "SF"; // Stock Fact
                    default:
                        return "";
                }
            }
        }

        public string OwnerCode
        {
            get
            {
                if (this.Owner == 9999)
                    return "S";
                else
                    return this.Owner.ToString();
            }
        }

        private int GetQuantityAvailableCatalog(int qtyAvail, int? packQty)
        {
            if (qtyAvail != null && packQty != null)
                return (int)Math.Floor((double)qtyAvail / (double)packQty);
            else
                return int.MinValue;
        }

        const string MIXED_TALL_PALLET = "Mixed Tall Pallet";
        const string MIXED_HALF_PALLET = "Mixed Half Pallet";
        const string MIXED_TALL_PALLET_CODE = "4AMP";
        const string MIXED_HALF_PALLET_CODE = "4MHP";
        const string HALF_PALLET = "Half Pallet";

        private string GetBOPackTypeMeaning(string _value)
        {
            if (string.IsNullOrEmpty(_value)) return string.Empty;

            switch (_value.ToUpper())
            {
                case "1PIE":
                    return "Pieces";
                case "2SET":
                    return "Sets";
                case "2UNT":
                    return "Units";
                case "3OUT":
                    return "Outers";

                case MIXED_TALL_PALLET_CODE: //Mixed Tall Pallet
                    return MIXED_TALL_PALLET;

                case MIXED_HALF_PALLET_CODE: //Mixed Half Pallet
                    return MIXED_HALF_PALLET;

                case "4HDP":
                    return HALF_PALLET; //Half Pallet

                case "5TSP":
                    return "TS Pallet"; //Tall Pallet

                case "6TDP":
                    return "TD Pallet";//Half Pallet

                case "7SDP":
                    return "SD Pallet";//Half Pallet

                case "8HCC":
                    return "Half Crate";
                default:
                    return _value.ToString();
            }
        }


        private string GetBOPackType2Meaning(string _value)
        {
            if (string.IsNullOrEmpty(_value)) return string.Empty;

            switch (_value.ToUpper())
            {
                case "1PIE":
                    return "Pieces";
                case "2SET":
                    return "Sets";
                case "2UNT":
                    return "Units";
                case "3OUT":
                    return "Outers";

                case MIXED_TALL_PALLET_CODE: //Mixed Tall Pallet
                    return MIXED_TALL_PALLET;

                case MIXED_HALF_PALLET_CODE: //Mixed Half Pallet
                    return MIXED_HALF_PALLET;

                case "4HDP":
                    return HALF_PALLET;

                case "5TSP":
                    return "Tall Pallets";

                case "8HCC":
                    return "Half Crate";

                case "6CRA":
                    return "Crate -A";
                case "6CRB":
                    return "Crate -B";
                case "6CRC":
                    return "Crate -C";
                case "6CRD":
                    return "Crate -D";
                case "6CRE":
                    return "Crate -E";
                default:
                    return _value.ToString();
            }
        }
    }
}
